# coding:utf-8

import os
import set_env_path     # 请勿删除

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
Session = sessionmaker(bind=engine)
session = Session()


class TypeToName(object):
    MODULE_GROUP = 1
    ORIGINAL_BOX = 2
    RENTAL_BOX = 3
    COMMON_BOX = 4
    BOX_FASTENER = 5
    POWER_SUPPLY = 6
    RECEIVE_CARD = 7
    ASYNC_RECEIVE_CARD = 8
    SENDING_CARD = 9
    ASYNC_SENDING_CARD = 10
    VIDEO_PROCESSOR = 11
    VIDEO_SPLICER = 12
    ASYNC_CONTROLLER = 13
    NETWORK_CARD = 14
    SCREW = 15
    STEEL_STRUCTURE = 16
    STEEL_STRUCTURE_HIGH = 17
    STEEL_STRUCTURE_WELDING = 18
    STEEL_STRUCTURE_TO_ALUMINUM = 19
    POWER_LINE = 21
    CABLE = 22
    THREE_M_RETICLE = 23
    ONE_AND_HALF_M_RETICLE = 24
    COMPUTER = 25
    EXHAUST_FAN = 26
    AIR_CONDITIONER = 27
    POWER_AMPLIFIER = 28
    SOUND_COLUMN = 29
    LIGHTNING_ROD = 30
    SECTION_BAR = 31
    SECTION_BAR_CORNER = 32
    SECTION_BAR_INTERFACE = 33
    PENDANT = 34
    SQUARE_PIPE = 35
    SPECIAL_SQUARE_PIPE = 36
    MAGNET = 37
    SECURITY = 38
    DISTRIBUTOR = 39

    mapping = {
        MODULE_GROUP: '模组',
        ORIGINAL_BOX: '原装箱体',
        RENTAL_BOX: '组装-租赁箱体',
        COMMON_BOX: '组装-普通标准箱体',
        BOX_FASTENER: '箱体扣件',
        POWER_SUPPLY: '电源',
        RECEIVE_CARD: '接收卡',
        ASYNC_RECEIVE_CARD: '异步接收卡',
        SENDING_CARD: '发送卡',
        ASYNC_SENDING_CARD: '异步发送卡',
        VIDEO_PROCESSOR: '视频处理器',
        VIDEO_SPLICER: '视频拼接器',
        ASYNC_CONTROLLER: '异步控制器',
        NETWORK_CARD: '网卡',
        SCREW: '螺丝',
        STEEL_STRUCTURE: '钢结构',
        STEEL_STRUCTURE_HIGH: '钢结构离地高度加价',
        STEEL_STRUCTURE_WELDING: '钢结构加焊不锈钢加价',
        STEEL_STRUCTURE_TO_ALUMINUM: '钢结构换成铝单板加价',
        POWER_LINE: '电源线',
        CABLE: '排线',
        THREE_M_RETICLE: '3米网线',
        ONE_AND_HALF_M_RETICLE: '1.5米网线',
        COMPUTER: '电脑',
        EXHAUST_FAN: '排气扇',
        AIR_CONDITIONER: '空调',
        POWER_AMPLIFIER: '功放',
        SOUND_COLUMN: '音柱',
        LIGHTNING_ROD: '避雷针',
        SECTION_BAR: '型材',
        SECTION_BAR_CORNER: '型材拐角',
        SECTION_BAR_INTERFACE: '型材接口',
        PENDANT: '挂件',
        SQUARE_PIPE: '方管',
        SPECIAL_SQUARE_PIPE: '特殊方管',
        MAGNET: '磁铁',
        SECURITY: '安防拼接器',
        DISTRIBUTOR: '配电柜'
    }


def sync_product_type_id():
    """
    create by ld_wu 2017-09-22
    由于把产品类修改成常量需要同步更新产品类的id
    """
    select_type_sql = """
    select id, name from product_types
    """

    update_product_sql = """
        update products set product_type_id=:type_id 
        where id=:id
    """

    update_spec_sql = """
        update product_specs set product_type_id=:type_id 
        where id=:id
    """

    select_products_sql = """
        select id, product_type_id from products
    """

    select_specs_sql = """
            select id, product_type_id from product_specs
        """

    try:
        types = session.execute(select_type_sql).fetchall()
        name_to_id = {value: key for key, value in TypeToName.mapping.items()}
        type_to_name = {each.id: each.name for each in types}
        products = session.execute(select_products_sql).fetchall()
        for each in products:
            session.execute(
                update_product_sql,
                {
                    'id': each.id,
                    'type_id': name_to_id[type_to_name[each.product_type_id]]
                }
            )
        specs = session.execute(select_specs_sql).fetchall()
        for each in specs:
            session.execute(
                update_spec_sql,
                {
                    'id': each.id,
                    'type_id': name_to_id[type_to_name[each.product_type_id]]
                }
            )
        session.commit()
    except Exception as e:
        print(e)
        session.rollback()


if __name__ == '__main__':
    sync_product_type_id()
